<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>ES6对象属性和方法的简写</title>
</head>

<body>
  <script>
    // ============================== ES6对象属性和方法的简写 ==============================
    // 1. 对象属性名与属性值的变量名同名, 可以简写, 只写一个
    // 2. 方法的简写， 方法直接将 :function 省略不写

    let name = 'zs'
    let age = 18
    let mydesc = '不错'

    // ================== 常规写法 ================== 
    let obj1 = {
      name: name,
      age: age,
      desc: mydesc,
      sayHi(){
        console.log('hello')  
      }
    }
    console.log(obj1)
    // ================== 简写形式 ==================

    let obj2 = {
      name,
      age,
      desc: mydesc
    }
    console.log(obj2)

    // let obj2 = {
    //   name,
    //   age,
    //   desc   //error  desc is not defined  因为属性名和属性值不同名，desc找不到 
    // }
    // console.log(obj2)





    // ================== 说明 ==================
    //  区分: 普通函数、箭头函数、方法简写
    let info = {
      sayHi1: function () {
        console.log(this)  // info
      },
      sayHi2() {
        console.log(this)  // info
      },
      sayHi3: () => {
        console.log(this)  // window
      }
    }

    info.sayHi1()
    info.sayHi2()
    info.sayHi3()
  </script>
</body>

</html>